package cn.com.ghy.android.bean;

import java.io.InputStream;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlSerializer;

import android.util.Xml;
import cn.com.ghy.android.util.XMLStatic;

/**
 * @软件著作权:金红叶纸业集团有限公司
 * @系统名称:SFA系统
 * @Description:今天协访小店信息解析器
 * @author wuz  @date 2011-6-21 16:49:25
 * @版本历史:v1.0
 * @Copyright:Copyright (c) 2011-2012 
 */

public class CoopVisitSmallShopInfoBeanParser {
	public static String createVsPartDataXMLForSmallShop(String vstype, CoopVisitSmallShopInfoBean bean,
			Writer writer) throws Exception {
        XmlSerializer xmlSerializer = Xml.newSerializer();
        
        xmlSerializer.setOutput(writer);
        xmlSerializer.startDocument("UTF-8", true);
        xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.attribute("", XMLStatic.TOTAL_RECORD, "1");
        xmlSerializer.startTag("", XMLStatic.RECORDS);
        
        xmlSerializer.startTag("", XMLStatic.VSTYPE);
        xmlSerializer.text(vstype);
		xmlSerializer.endTag("", XMLStatic.VSTYPE);
		
        xmlSerializer.startTag("", XMLStatic.PARTNER);
        xmlSerializer.text(bean.getPartner());
		xmlSerializer.endTag("", XMLStatic.PARTNER);
		xmlSerializer.startTag("", XMLStatic.VSDATE);
        xmlSerializer.text(bean.getVsdate());
        xmlSerializer.endTag("", XMLStatic.VSDATE);
        xmlSerializer.startTag("", XMLStatic.ISVISIT);
        xmlSerializer.text("");
		xmlSerializer.endTag("", XMLStatic.ISVISIT);
    	xmlSerializer.startTag("", XMLStatic.CONTENT);
        xmlSerializer.text(bean.getContent());
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		
		xmlSerializer.startTag("", XMLStatic.PERNR);
        xmlSerializer.text(bean.getPernr());
		xmlSerializer.endTag("", XMLStatic.PERNR);
		xmlSerializer.startTag("", XMLStatic.CUSTNUM);
        xmlSerializer.text(bean.getCustnum());
		xmlSerializer.endTag("", XMLStatic.CUSTNUM);
		xmlSerializer.startTag("", XMLStatic.SALENUM);
        xmlSerializer.text(bean.getSalenum());
		xmlSerializer.endTag("", XMLStatic.SALENUM);
		xmlSerializer.startTag("", XMLStatic.ISSALE);
        xmlSerializer.text(bean.getIssale());
		xmlSerializer.endTag("", XMLStatic.ISSALE);
		xmlSerializer.startTag("", XMLStatic.PJPVISIT);
        xmlSerializer.text(bean.getPjpvisit());
		xmlSerializer.endTag("", XMLStatic.PJPVISIT);
		xmlSerializer.startTag("", XMLStatic.PREPARE);
        xmlSerializer.text(bean.getPrepare());
		xmlSerializer.endTag("", XMLStatic.PREPARE);
		xmlSerializer.startTag("", XMLStatic.CUSTDISPLAY);
        xmlSerializer.text(bean.getCustdisplay());
		xmlSerializer.endTag("", XMLStatic.CUSTDISPLAY);
		xmlSerializer.startTag("", XMLStatic.FIRSTCMPTDISPLAY);
        xmlSerializer.text(bean.getFirstcmptdisplay());
		xmlSerializer.endTag("", XMLStatic.FIRSTCMPTDISPLAY);
		xmlSerializer.startTag("", XMLStatic.CHANNEL);
        xmlSerializer.text(null == bean.getChannel() ? "" : bean.getChannel());
		xmlSerializer.endTag("", XMLStatic.CHANNEL);
		xmlSerializer.startTag("", XMLStatic.REGARDCUST);
        xmlSerializer.text(bean.getRegardcust());
		xmlSerializer.endTag("", XMLStatic.REGARDCUST);
		xmlSerializer.startTag("", XMLStatic.CLEANSHELF);
        xmlSerializer.text(bean.getCleanshelf());
		xmlSerializer.endTag("", XMLStatic.CLEANSHELF);
		xmlSerializer.startTag("", XMLStatic.SDH);
        xmlSerializer.text(bean.getSdh());
		xmlSerializer.endTag("", XMLStatic.SDH);
		xmlSerializer.startTag("", XMLStatic.STOCK);
        xmlSerializer.text(bean.getStock());
		xmlSerializer.endTag("", XMLStatic.STOCK);
		xmlSerializer.startTag("", XMLStatic.RECORDER);
        xmlSerializer.text(bean.getRecorder());
		xmlSerializer.endTag("", XMLStatic.RECORDER);
		xmlSerializer.startTag("", XMLStatic.CHOOSESKU);
        xmlSerializer.text(null == bean.getChoosesku() ? "" : bean.getChoosesku());
		xmlSerializer.endTag("", XMLStatic.CHOOSESKU);
		xmlSerializer.startTag("", XMLStatic.EXPRESS);
        xmlSerializer.text(null == bean.getExpress() ? "" : bean.getExpress());
		xmlSerializer.endTag("", XMLStatic.EXPRESS);
		xmlSerializer.startTag("", XMLStatic.PERSU);
        xmlSerializer.text(null == bean.getPersu() ? "" : bean.getPersu());
		xmlSerializer.endTag("", XMLStatic.PERSU);
		xmlSerializer.startTag("", XMLStatic.THISBUY);
        xmlSerializer.text(bean.getThisbuy());
		xmlSerializer.endTag("", XMLStatic.THISBUY);
		xmlSerializer.startTag("", XMLStatic.CUSER);
        xmlSerializer.text(bean.getCuser());
		xmlSerializer.endTag("", XMLStatic.CUSER);
		
		xmlSerializer.endTag("", XMLStatic.RECORDS);
		xmlSerializer.endTag("", XMLStatic.CONTENT);
		xmlSerializer.endDocument();
		writer.flush();
		writer.close();
		return writer.toString().replace("<?xml version='1.0' encoding='UTF-8' standalone='yes' ?>", "");
	}
	
	public static List<CoopVisitSmallShopInfoBean> parseXML(InputStream in) throws Exception {
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(in, "UTF-8");
        int type = parser.getEventType(); 
        
        CoopVisitSmallShopInfoBean bean = null;
        List<CoopVisitSmallShopInfoBean> list = null;
        
        while (type != XmlPullParser.END_DOCUMENT) { 
            switch (type) {   
            case XmlPullParser.START_DOCUMENT:   
            	list = new ArrayList<CoopVisitSmallShopInfoBean>();   
                break;   
   
            case XmlPullParser.START_TAG:
                if ("Table".equals(parser.getName())) {
                	bean = new CoopVisitSmallShopInfoBean();
                } else if (null != list && null != bean) {
                    if ("PARTNER".equals(parser.getName())) {   
                    	bean.setPartner(parser.nextText());   
                    } else if("VSDATE".equals(parser.getName())) {
                    	bean.setVsdate(parser.nextText());
                    } else if("CONTENT".equals(parser.getName())) {
                    	bean.setContent(parser.nextText());
                    } else if("PERNR".equals(parser.getName())) {
                    	bean.setPernr(parser.nextText());
                    } else if("CUSTNUM".equals(parser.getName())) {
                    	bean.setCustnum(parser.nextText());
                    } else if("SALENUM".equals(parser.getName())) {
                    	bean.setSalenum(parser.nextText());
                    } else if("PJPVISIT".equals(parser.getName())) {
                    	bean.setPjpvisit(parser.nextText());
                    } else if("ENTERTIME".equals(parser.getName())) {
                    	bean.setEntertime(parser.nextText());
                    } else if("LEAVETIME".equals(parser.getName())) {
                    	bean.setLeavetime(parser.nextText());
                    } else if("PREPARE".equals(parser.getName())) {
                    	bean.setPrepare(parser.nextText());
                    } else if("ISSALE".equals(parser.getName())) {
                    	bean.setIssale(parser.nextText());
                    } else if("CUSTDISPLAY".equals(parser.getName())) {
                    	bean.setCustdisplay(parser.nextText());
                    } else if("CUSER".equals(parser.getName())) {
                    	bean.setCuser(parser.nextText());
                    } else if("FIRSTCMPTDISPLAY".equals(parser.getName())) {
                    	bean.setFirstcmptdisplay(parser.nextText());
                    } else if("ISVISIT".equals(parser.getName())) {
                    	bean.setIsvisit(parser.nextText());
                    } else if("CHANNEL".equals(parser.getName())) {
                    	bean.setChannel(parser.nextText());
                    } else if("REGARDCUST".equals(parser.getName())) {
                    	bean.setRegardcust(parser.nextText());
                    } else if("CLEANSHELF".equals(parser.getName())) {
                    	bean.setCleanshelf(parser.nextText());
                    } else if("SDH".equals(parser.getName())) {
                    	bean.setSdh(parser.nextText());
                    } else if("STOCK".equals(parser.getName())) {
                    	bean.setStock(parser.nextText());
                    } else if("RECORDER".equals(parser.getName())) {
                    	bean.setRecorder(parser.nextText());
                    } else if("CHOOSESKU".equals(parser.getName())) {
                    	bean.setChoosesku(parser.nextText());
                    } else if("EXPRESS".equals(parser.getName())) {
                    	bean.setExpress(parser.nextText());
                    } else if("PERSU".equals(parser.getName())) {
                    	bean.setPersu(parser.nextText());
                    } else if("THISBUY".equals(parser.getName())) {
                    	bean.setThisbuy(parser.nextText());
                    }
                }   
                break;   
   
            case XmlPullParser.END_TAG:   
                if ("Table".equals(parser.getName())) {
                	if (null != list && bean != null) {
                		list.add(bean);
                		bean = null;
                	} 
                }
                break;
            }
            type = parser.next();
        }   
        in.close();   
        return list;  
    }
}
